package com.example.nick.bodyapplication.anim

import android.content.Intent
import android.os.Bundle
import android.transition.ChangeBounds
import android.transition.Scene
import android.transition.Transition
import android.transition.TransitionManager
import android.view.View
import android.view.ViewGroup
import android.widget.Button
import androidx.appcompat.app.AppCompatActivity
import androidx.core.app.ActivityOptionsCompat
import com.example.nick.bodyapplication.R

class AnimaActivity : AppCompatActivity() {

    //场景的SceneRoot
    private lateinit var rootView: ViewGroup

    //两个布局的场景
    private lateinit var scene1: Scene
    private lateinit var scene2: Scene

    //开始按钮
    private lateinit var btnStart: View

    //返回按钮
    private lateinit var btnBack: View
    private lateinit var btn_begin: View
    private lateinit var imageView2: View

    //两个Transition
    private val transition1: Transition = ChangeBounds()
    private val transition2: Transition = ChangeBounds()


    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_sence1)

        rootView = findViewById(android.R.id.content)

        scene2 = Scene.getSceneForLayout(rootView, R.layout.activity_sence2, this)
        scene1 = Scene.getSceneForLayout(rootView, R.layout.activity_sence1, this)

        transition1.addListener(object : Transition.TransitionListener {
            override fun onTransitionStart(transition: Transition) {

            }

            override fun onTransitionEnd(transition: Transition) {
                scene2Setting()
            }

            override fun onTransitionCancel(transition: Transition) {

            }

            override fun onTransitionPause(transition: Transition) {

            }

            override fun onTransitionResume(transition: Transition) {

            }
        })
        transition2.addListener(object : Transition.TransitionListener {
            override fun onTransitionStart(transition: Transition) {

            }

            override fun onTransitionEnd(transition: Transition) {
                scene1Setting()
            }

            override fun onTransitionCancel(transition: Transition) {

            }

            override fun onTransitionPause(transition: Transition) {

            }

            override fun onTransitionResume(transition: Transition) {

            }
        })

        scene1Setting()
    }

    /**
     * 第一个场景的设置
     */
    private fun scene1Setting() {
        btnStart = rootView.findViewById(R.id.btn_start)
        btnStart.setOnClickListener {
            TransitionManager.go(scene2, transition1)
        }
        btn_begin = rootView.findViewById(R.id.btn_begin)
        imageView2 = rootView.findViewById(R.id.imageView2)
//        val fadeTransition = Fade(Fade.IN)
        btn_begin.setOnClickListener {
//            TransitionManager.beginDelayedTransition(rootView, fadeTransition)
            TransitionManager.beginDelayedTransition(rootView)
            imageView2.visibility =
                if (imageView2.visibility == View.GONE) View.VISIBLE else View.GONE
        }
        val btn_2 = rootView.findViewById<Button>(R.id.btn_2)
        btn_2.setOnClickListener {
            val intent = Intent(this, Anim2Activity::class.java)
            val bundle = ActivityOptionsCompat.makeSceneTransitionAnimation(
                this,
                androidx.core.util.Pair(imageView2, getString(R.string.ts_common_img))
            ).toBundle()
            startActivity(intent, bundle)
        }
        val btn_3 = rootView.findViewById<Button>(R.id.btn_3)
        btn_3.setOnClickListener {
            startActivity(Intent(this, MotionLayoutActivity::class.java))
        }
    }

    /**
     * 第二个场景的设置
     */
    private fun scene2Setting() {
        btnBack = rootView.findViewById(R.id.btn_back)
        btnBack.setOnClickListener {
            TransitionManager.go(scene1, transition2)

        }
    }
}